/*
 * Corey McGee
 * Data Analysis
 * CS 480/481
 */
package userInterface;

import java.io.File;
import javax.swing.filechooser.FileFilter;

public class ParsedFileFilter extends FileFilter{
	
	/**
	 * Checks whether or not a given file is of the desired file type
	 * Currently only accepts txt files
	 */
	public boolean accept(File f) {
		if(f.isDirectory()) { //allows user to navigate the file system
			return true;
		}
			
		String extension = getExtension(f); //grabs the file extension
		if(extension != null) {
			if(extension.equals("txt")) //if it's the desired file type, return true
				return true;
			else
				return false;
		}
		return false; //assume false by default
	}
	   
	/**
	 * Pulls the file extension from a given file
	 * @param f
	 * @return
	 */
	public String getExtension(File f) {
		String ext = null;
		String s = f.getName(); //grab the file name
		int i = s.lastIndexOf('.'); //find the beginning of the file's extension
			
		if(i >0 && i < s.length() - 1) {
			ext = s.substring(i+1).toLowerCase(); //pull out the entire extension
		}
			
		return ext; //return the extension
	}

	/**
	 * Not used
	 */
	@Override
	public String getDescription() {
		return null;
	}
}
